Page({
  data: {
    books: [],
    fullPath: "", // 选择的图片地址
    // ----------------
    keyword: "", // 关键字
    history: wx.getStorageSync("history") || [], // 关键字数组
  },
  // ---------------------------------------------
  searchByKeyword() {
    // 1.1 取出关键字存储到数组
    const { keyword, history } = this.data;
    if (!keyword.trim()) {
      return wx.showToast({
        title: "请输入要搜索的内容",
        icon: "none",
      });
    }
    history.push(keyword);
    // 1.2 更新视图
    this.setData(
      {
        history,
        keyword: "",
      },
      () => {
        // 1.3 存储数组到本地
        wx.setStorageSync("history", history);
      }
    );
  },
  clearHistory() {
    this.setData(
      {
        history: [],
      },
      () => {
        wx.removeStorageSync("history");
      }
    );
  },
  // ---------------------------------------------------
  // 1. 获取书单数据
  getBooks() {
    // 1.1 显示loading提示框
    wx.showLoading({
      title: "正在拼命加载中...",
      mask: true,
    });
    // 1.2 调用接口发请求
    wx.request({
      url: "https://hmajax.itheima.net/api/books",
      method: "GET",
      data: { creator: "yjh" },
      header: {},
      success: (res) => {
        console.log(res.data.data);
        // 更新数据
        this.setData({
          books: res.data.data,
        });
        // 1.4 提示用户
        wx.showToast({
          title: "获取成功",
          icon: "success",
        });
      },
      fail: () => {
        wx.showToast({
          title: "获取失败",
          icon: "error",
        });
      },
      complete: () => {
        // 1.3 关闭loading提示框
        wx.hideLoading();
      },
    });
  },
  // ------------------------
  // 2. 存数据(同步)
  // 往本地存储数据(可以是任意类型的数据, 不需要序列化和反序列化)
  setStorage() {
    wx.setStorageSync("user-name", "不知火舞");
    wx.setStorageSync("person", { name: "张三丰", age: 700, gender: "男" });
  },
  // 3. 读数据(同步)
  getStorage() {
    const userName = wx.getStorageSync("user-name");
    const person = wx.getStorageSync("person");
    console.log(userName, person);
  },
  // 4. 删数据(同步)
  delStorage() {
    wx.removeStorageSync("logs");
  },
  // 5. 清数据(同步)
  clearStorage() {
    wx.clearStorageSync();
  },
  // ----------异步操作-本地存储-begin--------------
  async setStorageAsync() {
    // wx.setStorage({
    //   key: "TOKEN",
    //   data: "ewewewcdfew3223232",
    //   success() {
    //     // 后续操作
    //     console.log("存储成功");
    //   },
    // });
    // ------------以promise风格调用-------------
    const res = await wx.setStorage({
      key: "ccccc",
      data: "666888787779",
    });
    console.log(res);
  },
  getStorageAsync() {
    wx.getStorage({
      key: "TOKEN",
      success(res) {
        console.log(res.data);
      },
    });
  },
  delStorageAsync() {
    wx.removeStorage({
      key: "TOKEN",
      success(res) {
        console.log(res);
      },
    });
  },
  clearStorageAsync() {
    wx.clearStorage({
      success() {
        console.log("删除成功");
      },
    });
  },
  // ----------异步操作-本地存储-end--------------
  // 图片上传预览
  async chooseImage() {
    //--------------- 写法2 async/await
    const res = await wx.chooseMedia({
      count: 1,
      mediaType: ["image"],
      sourceType: ["album"],
    });
    // console.log(res);
    this.setData({
      fullPath: res.tempFiles[0].tempFilePath,
    });
    //--------------- 写法1
    // wx.chooseMedia({
    //   count: 1,
    //   mediaType: ["image"],
    //   sourceType: ["album"],
    //   success: (res) => {
    //     console.log(res);
    //     this.setData({
    //       fullPath: res.tempFiles[0].tempFilePath,
    //     });
    //   },
    //   fail: () => {},
    //   complete: () => {},
    // });
  },
});
